package com.sgb.goods.widget;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;

import com.swgk.core.util.MLog;

import java.util.Timer;
import java.util.TimerTask;

public class TempAnimationUtils {

    private View view;
    private Timer timer = null;
    TimerTask task = null;
    private final int caleNum = 10;
    ViewGroup.MarginLayoutParams lp;
    /** 计数器 */
    private int counter = 5;
    private int maxNum = 0;
    private int type;
    private boolean taskIsRunning;

    public TempAnimationUtils() {

    }

    /**
     *
     * @param type    1：显示；2：隐藏；
     */
    public void startAnim(View view, int maxNum, int type) {
        if(null == view) {
            MLog.e("未设置目标视图对象，启动动画失败。");
            return;
        }
        if(maxNum <= 0) {
            MLog.e("未设置最大变化值，启动动画失败。");
            return;
        }
        if(taskIsRunning) {
            MLog.e("存在未执行完的任务，启动动画失败。");
            return;
        }

        reset();

        this.view = view;
        this.maxNum = maxNum;
        this.type = type;

        lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

        task = new TimerTask() {

            @Override
            public void run() {
                MLog.e("开始计数----", "type: " + type + " | " + "maxNum: "+maxNum + " | " + "counter："+counter);
                if(2 == type && maxNum >= counter) {
                    // 隐藏
                    counter += caleNum;
                    handler.sendEmptyMessage(-1);
                } else if(1 == type && 0 <= counter) {
                    // 显示
                    counter -= caleNum;
                    handler.obtainMessage(-1).sendToTarget();
                } else {
                    task.cancel();
                    taskIsRunning = false;
                }
            }
        };

        timer = new Timer();
        timer.schedule(task, 10, 10);
        taskIsRunning = true;
    }

    Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            animCalculate(type);
        }
    };

    /**
     * 动画计算
     * @param type    1：显示；2：隐藏；
     */
    private void animCalculate(int type) {
        // 动画计算
        lp.topMargin = 2 == type ? lp.topMargin - caleNum : lp.topMargin + caleNum;

        // 误差分析
        if(2 == type) {
            lp.topMargin = Math.abs(maxNum - lp.topMargin) <= caleNum ? maxNum : lp.topMargin;
        } else if(1 == type) {
            lp.topMargin = Math.abs(maxNum - lp.topMargin) <= caleNum ? 0 : lp.topMargin;
        }

        // 视图更新
        this.view.requestLayout();
    }

    /** 资源释放 */
    public void reset() {
        if(null != timer) {
            timer.cancel();
            timer.purge();
            timer = null;
        }
        if(null != task) {
            task.cancel();
            task = null;
        }
    }

}
